package ar.Location.UI;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import ar.Location.Map.EnhancedMapView;
import ar.Location.Map.InterestingLocations;
import ar.Location.PlaceObject.PlaceCategory;
import ar.activity.R;
import ar.activity.DirectionsActivity;
import ar.activity.PlaceDetailActivity;

import com.google.android.maps.GeoPoint;

public class PopUpMarkerView extends LinearLayout {

	Context popUpMarkerContext = null;
	TextView txtTitle = null;
	TextView txtSnippet = null;
	LinearLayout popUpView = null;
	OnClickListener onPopUpClick = null;
	ImageView markerClose = null;

	EnhancedMapView mapView = null;

	public GeoPoint geoPoint = null;

	public PopUpMarkerView(EnhancedMapView eMapView, Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		popUpMarkerContext = context;
		mapView = eMapView;
		initView();
	}

	public void initView() {

		inflate(popUpMarkerContext, R.layout.map_popup_marker, this);

		txtTitle = (TextView) findViewById(R.id.balloon_item_title);
		txtSnippet = (TextView) findViewById(R.id.balloon_item_snippet);
		markerClose = (ImageView) findViewById(R.id.balloon_close_button);
		markerClose.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				setVisibility(View.GONE);
			}
		});

		popUpView = (LinearLayout) findViewById(R.id.balloon_main_layout);
		onPopUpClick = new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (DirectionsActivity.modeDirections) {
					invisible();
					Intent intent = new Intent(popUpMarkerContext,
							DirectionsActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					popUpMarkerContext.startActivity(intent);
				} else {
					PlaceCategory.currentPlace = InterestingLocations.currentOverlayItem.place;
					if (PlaceCategory.currentPlace != null) {
						Intent intent = new Intent(popUpMarkerContext,
								PlaceDetailActivity.class);
						popUpMarkerContext.startActivity(intent);
					}
				}
			}
		};
		popUpView.setOnClickListener(onPopUpClick);
	}

	public void invisible() {
		setVisibility(View.GONE);
	}

	public void visible() {
		setVisibility(View.VISIBLE);
	}

	public void updateContent(String title, String snippet) {
		txtTitle.setText(title);
		txtSnippet.setText(snippet);
		if (txtSnippet.getText().length() == 0) {
			txtSnippet.setVisibility(View.GONE);
		}
	}
}
